#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/7/31 10:02
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 219. 存在重复元素 II.py
# @Software: PyCharm


class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        l = len(nums)
        m = {}
        for i in range(l):
            key = nums[i]
            if m.get(key) != None:
                io = m[key]
                if i-io <= k:
                    return True
            m[key] = i
        return False


def run(nums, k):
    s = Solution()
    ans = s.containsNearbyDuplicate(nums, k)
    print('输入: nums =', nums, ', k =', k)
    print('输出:', ans)


if __name__ == '__main__':

    nums = [1, 2, 3, 1]
    k = 3
    run(nums, k)

    nums = [1, 0, 1, 1]
    k = 1
    run(nums, k)

    nums = [1, 2, 3, 1, 2, 3]
    k = 2
    run(nums, k)
